﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;
using HRMS_Model;
using HRMS.Models;
using System.Globalization;
using Lib.Web.Mvc.JQuery.JqGrid;


namespace HRMS.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        public ActionResult Index()
        {
            return View();
        }

        //[HttpPost]
        //public ActionResult Index(Role role)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var rol = new Role
        //        {
        //           RoleType = role.RoleType

        //        };

        //        Roles.AddRole(role);
        //        return RedirectToAction("Index", "Role");
        //    }
        //    return View(role);
        //}

        //public ActionResult RoleData(string rows, string page, string sidx, string sord)
        //{
        //    var roles = HRMS.Models.Roles.GetRoles().ToList();
        //    var totalRecords = Convert.ToInt32(roles.Count());
        //    var jsonData = new
        //    {
        //        total = 1,
        //        page = Convert.ToInt32(page),
        //        records = totalRecords,
        //        rows = (
        //          from role in roles
        //          select new
        //          {
        //              id = role.RoleId,
        //              cell = new string[] {                         
        //                  role.RoleType
        //                    }
        //          })
        //    };
        //    return Json(jsonData, JsonRequestBehavior.AllowGet);
        //}

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Roles(JqGridRequest request)
        {
            //get the count of roles
            var totalRecords = RoleModels.GetRolesCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get roles
            var roles = RoleModels.GetRoles(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var role in roles)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(role.RoleId), new List<object>() { 
                                    role.RoleId,
                role.RoleType
                }));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertRole(Role roleModel)
        {
            return Json(RoleModels.AddRole(roleModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateRole([ModelBinder(typeof(CustomModelBinder))]Role roleModel)
        {
            return Json(RoleModels.UpdateRole(roleModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteRole(int id)
        {
            return Json(RoleModels.DeleteRole(id));
        }

        public ActionResult RoleNames()
         {
             return Json(RoleModels.RoleNames(), JsonRequestBehavior.AllowGet);
         }
    }
}
 